package cn.sofwin.project.src.utils;

import cn.sofwin.project.src.entity.*;
import cn.sofwin.project.src.entity.ConsumInfo;
import cn.sofwin.project.src.service.NetService;
import java.io.*;
import java.util.*;
/**
 *
 * @author 啊剥了菠萝没有菠萝蜜
 */
// 卡业务
public class CardUtil {
    Scanner sc = new Scanner(System.in);
    // 用户列表
    public Map<String, MobileCard> card = new HashMap<>();
    // 卡消费记录列表
    public Map<String, List<ConsumInfo>> consumInfos = new HashMap<>();

    // 初始化
    public void initScene(){
        String cardNumber = createNumber();
        MobileCard cardCreate = new MobileCard();
        if (isExistCard(cardNumber)) {// 不重复注册卡号
            initScene();
        }else {
            cardCreate.cardNumber = cardNumber;
            card.put(cardNumber, cardCreate);
            ConsumInfo consumInfo = new ConsumInfo();
            List<ConsumInfo> cI = new ArrayList<>();
            cI.add(consumInfo);
            consumInfos.put(cardNumber, cI);
            if (isExistCard(cardNumber)) {
                System.out.print("注册成功!");
                card.get(cardNumber).showMeg();
            }
        }
    }
    // 卡是否存在
    public boolean isExistCard(String number, String passWord){
        if (card.get(number) == null) {
            System.out.println("账号不存在");
            return false;
        }
        return true;
    }
    public boolean isExistCard(String number){
        if (card.get(number) == null) {
            System.out.println("卡号未注册");
            return false;
        }
        return true;
    }
    // 开号
    public String createNumber(){
        String[] randNumber = new String[9];
        Random random = new Random();
        System.out.println("*****可选择的卡号*****");
        // 随机生成9个新号码
        for (int i = 0; i < 9; i++) {
            randNumber[i] = "139";
            for (int j = 0; j < 8; j++) {
                randNumber[i] += String.valueOf(random.nextInt(10));
            }
        }
        // 打印9个号码
        for (int i = 0; i < 9; i++) {
            System.out.print((i+1) + "." + randNumber[i]);
            if (((i+1)%3) == 0) System.out.println();
            else System.out.print("\t");
        }
        System.out.print("请选择卡号（输入1~9的序号）：");
        return randNumber[sc.nextInt()-1];
    }
    // 获得新号码
//    public String[] getNewNumbers(int count){
//        return "11111";
//    }
    // 增加卡号
    public void addCard(MobileCard card){

    }
    // 退网
    public void delCard(String number){

    }
    // 显示余量信息
    public void showRemainDetail(String number){
        System.out.println("*****套餐余量查询******");
        System.out.println("您的卡号是" + number + "，套餐内剩余：");
        card.get(number).setPackage.showInfo();
    }
    // 显示账单信息
    public void showAmountDetail(String number){
        System.out.println("*****本月账单查询******");
        System.out.println("您的卡号：" + number + "，当月账单：");
        System.out.println("套餐资费" + card.get(number).setPackage.price + "元");
        System.out.println("合计：" + (card.get(number).setPackage.price + card.get(number).consumAmount) + "元");
        System.out.println("账户余额：" + card.get(number).money);
    }
    // 使用soso
    public void useSoso(String number){
        // 消费更改 余额信息 和 消费记录
        // 随机生成一个一位数，代表一个功能
        Random random = new Random();
        ConsumInfo consumInfo = new ConsumInfo();
        switch (random.nextInt(3)){
            case 0:
                Scene.Call(30, card, number);
                consumInfo.Number = consumInfos.get(number).size();
                consumInfo.type = "通话";
                consumInfo.consumData = 30;
                consumInfos.get(number).add(consumInfo);
                System.out.println("已添加一条消费记录。");
                break;
            case 1:
                if (!(card.get(number).setPackage instanceof NetPackage)) {
                    Scene.Send(10, card, number);
                    consumInfo.Number = consumInfos.get(number).size();
                    consumInfo.type = "短信";
                    consumInfo.consumData = 10;
                    consumInfos.get(number).add(consumInfo);
                    System.out.println("已添加一条消费记录。");
                }else {
                    useSoso(number);
                }
                break;
            case 2:
                if (!(card.get(number).setPackage instanceof TalkPackage)){
                    Scene.Net(0.2, card, number);
                    consumInfo.Number = consumInfos.get(number).size();
                    consumInfo.type = "上网";
                    consumInfo.consumData = 10;
                    consumInfos.get(number).add(consumInfo);
                    System.out.println("已添加一条消费记录。");
                }else {
                    useSoso(number);
                }
                break;
        }

    }
    // 显示描述
    public void showDescription(){
        StringBuilder result = new StringBuilder();
        String filePath = "D:\\dev\\java\\works\\java2021\\src\\cn\\sofwin\\project\\套餐资费说明.txt";
        try {
            BufferedReader in = new BufferedReader(
                    new InputStreamReader(
                            new FileInputStream(filePath), "gbk"));
            String str;
            System.out.println("*****资费说明******");
            while ((str = in.readLine()) != null){
                System.out.println(str);
            }
            System.out.println(str);
            in.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    // 更改套餐
    public void changingPack(String number){
        System.out.println("*****套餐变更******");
        System.out.print("1.话唠套餐  2.网虫套餐  3.超人套餐  请选择（序号）：");
        switch (sc.nextInt()){
            case 1:
                if (!(card.get(number).setPackage instanceof TalkPackage)){
                    if (card.get(number).money >= 58){
                        TalkPackage talkPackage1 = new TalkPackage();
                        card.get(number).setPackage = talkPackage1;
                        card.get(number).setPackage.price = 58;
                        card.get(number).consumAmount += 58;
                        card.get(number).money -= 58;
                        System.out.println("变更成功");
                    }else
                        System.out.println("对不起，您的余额不足以支付新套餐本月资费，请充值后再办理更换套餐业务！");
                }else
                    System.out.println("您的套餐就是话唠套餐，不需要变更");
                break;
            case 2:
                if (!(card.get(number).setPackage instanceof NetPackage)) {
                    if (card.get(number).money >= 68) {
                        NetService netPackage = new NetPackage();
                        card.get(number).setPackage = (ServicePackage) netPackage;
                        card.get(number).setPackage.price = 68;
                        card.get(number).consumAmount += 68;
                        card.get(number).money -= 68;
                        System.out.println("变更成功");
                    } else
                        System.out.println("对不起，您的余额不足以支付新套餐本月资费，请充值后再办理更换套餐业务！");
                }else
                    System.out.println("您的套餐就是网虫套餐，不需要变更");
                break;
            case 3:
                if (!(card.get(number).setPackage instanceof SuperPackage)) {
                    if (card.get(number).money >= 78) {
                        SuperPackage superPackage1 = new SuperPackage();
                        card.get(number).setPackage = superPackage1;
                        card.get(number).setPackage.price = 78;
                        card.get(number).consumAmount += 78;
                        card.get(number).money -= 78;
                        System.out.println("变更成功");
                    } else
                        System.out.println("对不起，您的余额不足以支付新套餐本月资费，请充值后再办理更换套餐业务！");
                }else
                    System.out.println("您的套餐就是超人套餐，不需要变更");
                break;
            default:
                System.out.println("输入错误");
        }
    }
    // 打印消费详单
    public void printConsumInfo(String number){
        System.out.println("******"+number+"******\n");
        System.out.println("序号\t\t类型\t\t数据（通话（条）/上网（MB）/短信（条））");
        List<ConsumInfo> value = consumInfos.get(number);
        for (int i = 1; i < value.size(); i++) {
            System.out.println(value.get(i).Number + ".\t\t" + value.get(i).type + "\t\t" + value.get(i).consumData);
        }
    }
    // 充话费
    public void chargeMoney(String number, double money){
        if (money < 50){
            System.out.println("对不起，最低充值金额为50元！");
            return;
        }
        if (!isExistCard(number)){
            System.out.println("对不起，要充值的卡号未注册，无法充值！");
            return;
        }
        card.get(number).money += money;
        System.out.println("充值成功，当前话费余额为" + card.get(number).money+"元。");
    }
}
